<?php
/**
 * Text element
 *
 */
class ArrowFormSwitch extends ArrowFormField{
	
	public function configure(){
		parent::configure();	
		$this->addProperties(array(
			"default" => false,
			"selected" => false
		));
	}	
	
	public function init(){
		parent::init();
		$jsData = array();
		$this->registerJs( get_class($this), dirname(__FILE__)."/ArrowFormSwitch.js", '#ctrl-form-switch-'.$this->getId(), $jsData);
	}

	public function validate( $proposedValue ){
		return true;
	}

	public function getValue(){
		if( $value = $this->getProperty("selected") )
			return $value;
		return parent::getValue();
	}
	
	public function generateOutput(){
		$attribuites = array();
		$attribuites["name"] = $this->getHtmlName();
		
		
		$str =  '<div id="ctrl-form-switch-'.$this->getId().'" >';
		$value = $this->getValue();
		$column = $this->getProperty("column");
		
		$counter = 0;
		
		$options = $this->getChildren("ArrowFormOption");
		foreach($options as $option){
			$currValue = $option->getValue();
			$selected = false;
			if($value !== null && $currValue == $value)
				$selected = true;
			elseif( $value === null && $option->isSelected() )
				$selected = true;
				
			$attribuites["id"] = "radio-".$this->getName()."-".$counter;
			$attribuites["value"] = $currValue;
			$checked = ($selected)?'checked="checked"':'';
			$str.= '<input type="radio" '.$this->genTagsFromArray($attribuites).''.$checked.' '.$this->addExternal().'/>';
			$str.= ' <label for="'.$attribuites["id"].'">'.$option->getLabel().'</label> ';
			
			$counter++;
		}
		
		
		$content = $this->getContent();
		
		if($content){
			$i = 1;
			foreach( $content as $key => $text ){
				$attribuites["id"] = "radio-".$this->getName()."-".$counter;
				$attribuites["value"] = $key;
				$checked = ($value==$key)?'checked="checked"':'';
				$str.= '<input type="radio" '.$this->genTagsFromArray($attribuites).''.$checked.' '.$this->addExternal().'/>';
				$str.= ' <label for="'.$attribuites["id"].'">'.$text.'</label> ';
				$counter++;
			}
		}
		return $str."</div>";
	}


}
?>